import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../models/plist_model.dart';
import '../../../services/httpsClient.dart';

class ProductListController extends GetxController {
  final count = 0.obs;
  bool flag = true;
  ScrollController scrollController = ScrollController();

  HttpsClient httpsClient = HttpsClient();

  /*二级导航数据*/
  List subHeaderList = [
    {
      "id": 1,
      "title": "综合",
      "fileds": "all",
      "sort":
          -1, // 排序     升序：price_1     {price:1}        降序：price_-1   {price:-1}
    },
    {"id": 2, "title": "销量", "fileds": 'salecount', "sort": -1},
    {"id": 3, "title": "价格", "fileds": 'price', "sort": -1},
    {"id": 4, "title": "筛选"}
  ];

  // 二级导航选中判断
  RxInt selectHeaderId = 1.obs;

  RxList<PlistItemModel> plist = <PlistItemModel>[].obs;
  GlobalKey<ScaffoldState> globalKey = GlobalKey<ScaffoldState>();
  RxInt page = 1.obs;
  int pageSize = 10;
  RxBool hasData = false.obs;
  String sort = '';
  // 获取传值
  String keyWords = Get.arguments['keyWords'];
  String cid = Get.arguments['id'];
  String apiUrl = "";
  // 主要解决排序箭头无法更新
  RxInt subHeaderListSort = 1.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getPlistData();
    initScrollController();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void getPlistData() async {
    if (flag && hasData.value) {
      flag = false;
      if (cid != null) {
        apiUrl =
            "api/plist?cid=$cid&page=${page.value}&pageSize=$pageSize&sort=$sort";
      } else {
        apiUrl =
            "api/plist?search=$keyWords&page=${page.value}&pageSize=$pageSize&sort=$sort";
      }
      var res = await httpsClient.get(apiUrl);

      if (res != null) {
        var plistTemp = PlistModel.fromJson(res.data);
        // 拼接数据
        plist.addAll(plistTemp.result);
        page.value++;
        update();
        flag = true;
        if (plistTemp.result.length < pageSize) {
          hasData.value = false;
        }
      }
    }
  }

  // 绑定滚动事件
  void initScrollController() {
    scrollController.addListener(() {
      // 滚动下拉高度
      // scrollController.position.pixels;
      // 页面的高度
      // scrollController.position.maxScrollExtent;
      if (scrollController.position.pixels >
          scrollController.position.maxScrollExtent - 50) {
        getPlistData();
      }
    });
  }

  // 二级导航改变
  void subHeaderChange(id) {
    if (id == 4) {
      globalKey.currentState!.openEndDrawer();
    }
    selectHeaderId.value = id;

    // 定义排序
    sort =
        "${subHeaderList[id - 1]['fileds']}_${subHeaderList[id - 1]['sort']}";
    // 改变状态
    subHeaderList[id - 1]['sort'] = subHeaderList[id - 1]['sort'] * -1;
    // 更新状态
    subHeaderListSort.value = subHeaderList[id - 1]['sort'];

    // 重置数据
    page.value = 1;
    plist.value = [];
    hasData.value = true;
    flag = true;

    // 滚动条回到顶部
    scrollController.jumpTo(0);

    // 请求接口
    getPlistData();
    update();
  }
}
